package org.liveboardgames.agricola.view.component
{
    import flash.events.Event;
    import flash.events.MouseEvent;
    
    import mx.controls.Image;
    import mx.core.DragSource;
    import mx.events.DragEvent;
    import mx.managers.DragManager;
    
    import org.liveboardgames.agricola.model.enums.AbstractEnum;
    import org.liveboardgames.agricola.model.enums.ResourceTypeEnum;
    import org.liveboardgames.agricola.view.event.BubblingDragEvent;
    
    public class ResourceImage extends Image
    {
        public var displayEmptyResources:Boolean = false;
        
        public var allowDrag:Boolean = false;
        
        private var _quantity:int;
        
        private var _resourceType:String;
        
        public function ResourceImage(allowDrag:Boolean = false, size:Number = 0)
        {
            super();
            this.setStyle("horizontalCenter", "0");
            this.setStyle("verticalCenter", "0");
            this.width = size;
            if (allowDrag)
            {
                this.addEventListener(MouseEvent.MOUSE_DOWN, dragIt, false, 0, true);
                this.addEventListener(DragEvent.DRAG_COMPLETE, onDragComplete, false, 0, true);
            }
        }
        
        override public function set width(value:Number):void
        {
            super.width = value;
            super.height = value;
        }
        
        override public function set height(value:Number):void
        {
            super.width = value;
            super.height = value;
        }
        
        public function set quantity(value:int):void
        {
            this._quantity = value;
            updateToolTip();
            updateSource();
            dispatchEvent(new Event("resourceChoiceModified", true));
        }
        
        public function get quantity():int
        {
            return this._quantity;
        }
        
        public function set resourceType(value:String):void
        {
            this._resourceType = value;
            updateToolTip();
            updateSource();
        }
        
        public function get resourceType():String
        {
            return this._resourceType;
        }
        
        private function updateToolTip():void
        {
            if (_resourceType != null)
            {
                this.toolTip = this._quantity + " " + AbstractEnum.composedWordToTitleCase(this._resourceType);
            }
        }
        
        private function updateSource():void
        {
            if (_resourceType != null)
            {
                if (_quantity > 0)
                {
                    this.source = ImageHandler.RESOURCES_ROOT_PATH + AbstractEnum.composedWordToTitleCase(this._resourceType) + "_" + Math.min(_quantity, 6) +
                        ".png";
                }
                else if (displayEmptyResources)
                {
                    this.source = ImageHandler.RESOURCES_ROOT_PATH + AbstractEnum.composedWordToTitleCase(this._resourceType) + "_0.png";
                }
            }
        
        }
        
        private function clone():ResourceImage
        {
            var clone:ResourceImage = new ResourceImage();
            
            clone._quantity = _quantity;
            clone._resourceType = _resourceType;
            clone.displayEmptyResources = displayEmptyResources;
            clone.allowDrag = allowDrag;
            
            return clone;
        }
        
        private function dragIt(event:MouseEvent):void
        {
            // Get the drag initiator component from the event object.
            var dragInitiator:ResourceImage = event.currentTarget as ResourceImage;
            
            // Create a DragSource object.
            var dragSource:DragSource = new DragSource();
            
            // Add the data to the object.
            var clone:ResourceImage = dragInitiator.clone();
            clone.quantity = 1;
            
            dragSource.addData(dragInitiator, 'originalComponent');
            dragSource.addData(clone, 'resource');
            if (ResourceTypeEnum.ANIMALS.contains(dragInitiator.resourceType))
            {
                dragSource.addData(clone, 'animal');
            }
            dragSource.addData(clone, dragInitiator.resourceType.toLowerCase());
            
            // Create a copy of the image to use as a drag proxy. Move only one peep at a time
            var dragProxy:ResourceImage = new ResourceImage(true);
            dragProxy.width = dragInitiator.width;
            dragProxy.resourceType = dragInitiator.resourceType;
            dragProxy.quantity = 1;
            
            // Call the DragManager doDrag() method to start the drag.
            DragManager.doDrag(dragInitiator, dragSource, event, dragProxy);
        }
        
        private function onDragComplete(event:DragEvent):void
        {
            // Prevent infinite loop
            if (!(event is BubblingDragEvent))
            {
                dispatchEvent(new BubblingDragEvent(event));
            }
        }
    }
}